package com.beiing.tianshuai.tianshuai.retrofit;


import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicBean;
import com.beiing.tianshuai.tianshuai.entity.MyAttentionBean;
import com.beiing.tianshuai.tianshuai.entity.MyCollectionBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalActivityBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalDynamicBean;
import com.beiing.tianshuai.tianshuai.entity.RecommendActivityInfoBean;
import com.beiing.tianshuai.tianshuai.entity.SignUpBody;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Jinni on 2017/7/11 0011.
 */

public class ApiServiceManager {

    private static Retrofit createRetrofit(String baseUrl) {
        OkHttpClient client = new OkHttpClient.Builder().
                connectTimeout(6, TimeUnit.SECONDS).
                readTimeout(6, TimeUnit.SECONDS).
                writeTimeout(6, TimeUnit.SECONDS).build();
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    /**
     * 请求获取注册结果
     *
     * @param baseUrl 接口地址
     * @param code    验证码
     * @param email   邮箱
     * @param name    昵称
     * @param pass    密码
     * @param addTime 注册时间
     * @return Observable
     */
    public static Observable<CommonBean> getSignUpResult(String baseUrl, String code, String email, String name, String pass, String addTime) {
        Retrofit retrofit = createRetrofit(baseUrl);
        ApiService apiService = retrofit.create(ApiService.class);
        LogUtils.i(code + "\n" + email + "\n" + name + "\n" + pass + "\n" + addTime, "getSignUpResult");
        SignUpBody signUpBody = new SignUpBody();
        signUpBody.setApp(HttpRequestConstant.REQUEST_APP_KEY);
        signUpBody.setCode(code);
        signUpBody.setEmail(email);
        signUpBody.setPass(pass);
        signUpBody.setName(name);
        signUpBody.setAddtime(addTime);
        return apiService.getSignUpResult(signUpBody);
    }

    /**
     * 请求获取验证码
     *
     * @param baseUrl 接口地址
     * @param email   邮箱
     * @return Observable
     */
    public static Observable<CommonBean> getSignUpCode(String baseUrl, String email) {
        Retrofit retrofit = createRetrofit(baseUrl);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getSignUpCode(
                email
        );
    }

    /**
     * 请求我关注的用户列表结果
     *
     * @param baseUrl 接口地址
     * @param id      用户id
     * @return Observable
     */
    public static Observable<MyAttentionBean> getMyAttentionResult(String baseUrl, String id) {
        Retrofit retrofit = createRetrofit(baseUrl);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getMyAttentionResult(
                id
        );
    }

    /**
     * 请求我粉丝的用户列表结果
     *
     * @param mainRequestHead 接口地址
     * @param id              用户id
     * @return Observable
     */
    public static Observable<MyAttentionBean> getMyFansResult(String mainRequestHead, String id) {
        Retrofit retrofit = createRetrofit(mainRequestHead);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getMyFansResult(
                id
        );
    }

    /**
     * 请求我的收藏列表结果
     *
     * @param mainRequestHead 地址接口
     * @param id              用户id
     * @return Observable
     */
    public static Observable<MyCollectionBean> getMyCollectionResult(String mainRequestHead, String id) {
        Retrofit retrofit = createRetrofit(mainRequestHead);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getMyCollectionResult(
                id
        );
    }

    /**
     * 请求动态列表结果
     *
     * @param mainRequestHead 地址接口
     * @param id              用户id
     * @param start           条目起始位置
     * @param num             请求条目数量
     * @return Observable
     */
    public static Observable<DynamicBean> getDynamicsResult(String mainRequestHead, String id, int start, int num) {
        Retrofit retrofit = createRetrofit(mainRequestHead);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getDynamicsResult(
                id,
                start,
                num
        );
    }

    public static Observable<PersonalDynamicBean> getPersonalDynamicsResult(String mainRequestHead, String uid, String id, int lastItemNum, int pageSize) {
        Retrofit retrofit = createRetrofit(mainRequestHead);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getPersonalDynamicsResult(
                uid,
                id,
                lastItemNum,
                pageSize
        );
    }

    public static Observable<PersonalActivityBean> getPersonalActivityResult(String mainRequestHead, String uid) {
        Retrofit retrofit = createRetrofit(mainRequestHead);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getPersonalActivityResult(
                uid
        );
    }


    /**
     * 获取活动专区信息
     *
     * @return 结果
     */
    public static Observable<RecommendActivityInfoBean> getRecommendActivityInfo(String url) {
        Retrofit retrofit = createRetrofit(url);
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService.getRecommendActivityInfo();
    }
}
